package com.zoo.springbootinit.manager;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zoo.springbootinit.common.ErrorCode;
import com.zoo.springbootinit.dasource.*;
import com.zoo.springbootinit.exception.BusinessException;
import com.zoo.springbootinit.model.dto.search.SearchRequest;
import com.zoo.springbootinit.model.entity.Picture;
import com.zoo.springbootinit.model.entity.Post;
import com.zoo.springbootinit.model.enums.SearchTypeEnum;
import com.zoo.springbootinit.model.vo.PostVO;
import com.zoo.springbootinit.model.vo.SearchVO;
import com.zoo.springbootinit.model.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;

@Component
@Slf4j
public class SearchFacade {
    @Autowired
    PictureDataSourceAccessor pictureDataSourceAccessor;
    @Autowired
    UserDataSourceAccessor userDataSourceAccessor;
    @Autowired
    PostDataSourceAccessor postDataSourceAccessor;
    @Autowired
    DataSourceAccessorRegister dataSourceAccessorRegister;


    public SearchVO searchAll(SearchRequest searchRequest) {
        //组织请求数据
        String searchText = searchRequest.getSearchText();
        String type = searchRequest.getType();
        SearchTypeEnum enumByValue = SearchTypeEnum.getEnumByValue(type);//type为空或参数不正确
        long current = searchRequest.getCurrent();
        long pageSize = searchRequest.getPageSize();
        //enumByValue为空,说明没有明确type类型,查询所有数据
        if (enumByValue == null) {

            CompletableFuture<Page<UserVO>> userTask = CompletableFuture.supplyAsync(() ->
                    userDataSourceAccessor.getDataSource(searchText, current, pageSize));

            CompletableFuture<Page<Picture>> pictureTask = CompletableFuture.supplyAsync(() ->
                    pictureDataSourceAccessor.getDataSource(searchText, current, pageSize));

            CompletableFuture<Page<PostVO>> postTask = CompletableFuture.supplyAsync(() ->
                    postDataSourceAccessor.getDataSource(searchText, current, pageSize));

            CompletableFuture.allOf(userTask, pictureTask, postTask).join();


            CompletableFuture.allOf(userTask, pictureTask,postTask).join();

            try {
                SearchVO searchVO = new SearchVO();
                Page<UserVO> userVOPage = userTask.get();
                Page<Picture> picturePage = pictureTask.get();
                Page<PostVO> postVOPage = postTask.get();
                searchVO.setUserList(userVOPage.getRecords());
                searchVO.setPictureList(picturePage.getRecords());
                searchVO.setPostList(postVOPage.getRecords());
                return searchVO;
            } catch (Exception e) {
                log.error("查询数据失败", e);
                throw new BusinessException(ErrorCode.SYSTEM_ERROR, "查询异常");
            }
        }

        //根据type查询
        else {
            SearchVO searchVO = new SearchVO();
            DataSourceAccessor<?> dataSourceAccessor = dataSourceAccessorRegister.getDataSourceByType(type);
            Page<?> dataSource = dataSourceAccessor.getDataSource(searchText, current, pageSize);
            searchVO.setDataList(dataSource.getRecords());
            return searchVO;
        }
    }
}
